<?php 
/**
*  curl：使用url语法传输数据的命令行工具
*  使用场景：
*	1.网页资源：编写网页爬虫，
*	2.WebService 数据接口资源：动态获取接口数据
*	3.FTP服务器里面的文件资源：下载FTP服务器里面的文件
*	4.其他资源：所有网络上的资源都可以用CURL访问和下载到
*/


//要切换到命令行界面 使用 php -f 命令运行
//基础用法
/*
$curl = curl_init("http://www.baidu.com");
curl_exec($curl);
curl_close($curl);
*/


//案例一  替换字符串
/*
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.baidu.com");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  //执行之后不打印出来
$output=curl_exec($curl);
curl_close($curl);
echo str_replace("百度", "屌丝", $output);
*/


//案例二 webservice简单使用

/*
$data = 'theCityName=上海';
$curlobj = curl_init();	
curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName");  
curl_setopt($curlobj, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36');
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curlobj, CURLOPT_POST, 1);  
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);  
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", 
	"Content-length: ".strlen($data)
	)); 
$rtn = curl_exec($curlobj);   
if(!curl_errno($curlobj)){
	// $info = curl_getinfo($curlobj); 
	// print_r($info);
	echo $rtn;  
} else {
  echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);
*/


//案例三   登陆并下载个人页面

/*
$data=array('username' => 'promonkey', 
	'password' => '1q2w3e',
	'remember'=>1);
$data='username=zjzhoufy@126.com&password=1q2w3e&remember=1';
$curlobj = curl_init();			// 初始化
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/user/login");		// 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);			// 执行之后不直接打印出来

// Cookie相关设置，这部分设置需要在所有会话开始之前设置
date_default_timezone_set('PRC'); // 使用Cookie时，必须先设置时区
curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($curlobj, CURLOPT_COOKIEFILE, 'cookiefile');
curl_setopt($curlobj, CURLOPT_COOKIEJAR, 'cookiefile');
curl_setopt($curlobj, CURLOPT_COOKIE, session_name() . '=' . session_id()); 
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); // 这样能够让cURL支持页面链接跳转

curl_setopt($curlobj, CURLOPT_POST, 1);  
curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);  
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", 
	"Content-length: ".strlen($data)
	)); 
curl_exec($curlobj);	// 执行
curl_setopt($curlobj, CURLOPT_URL, "http://www.imooc.com/space/index");
curl_setopt($curlobj, CURLOPT_POST, 0);  
curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("Content-type: text/xml"
	)); 
$output=curl_exec($curlobj);	// 执行
curl_close($curlobj);			// 关闭cURL
echo $output;

*/


//案例四  从FTP下载文件


/*
$curlobj = curl_init();	
curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/downloaddemo.txt");  
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//FTP用户名：密码
// Sets up the output file
$outfile = fopen('dest.txt', 'wb');//保存到本地的文件名
curl_setopt($curlobj, CURLOPT_FILE, $outfile);

$rtn = curl_exec($curlobj);  
fclose($outfile); 
if(!curl_errno($curlobj)){
	// $info = curl_getinfo($curlobj); 
	// print_r($info);
	echo "RETURN: " . $rtn;  
} else {
  echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);

*/


//案例五  上传文件到FTP服务器

/*
$curlobj = curl_init();	
$localfile = 'ftp01.php';
$fp = fopen($localfile, 'r');

curl_setopt($curlobj, CURLOPT_URL, "ftp://192.168.1.100/ftp01_uploaded.php");  
curl_setopt($curlobj, CURLOPT_HEADER, 0); 
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); // times out after 300s
curl_setopt($curlobj, CURLOPT_USERPWD, "peter.zhou:123456");//FTP用户名：密码

curl_setopt($curlobj, CURLOPT_UPLOAD, 1);
curl_setopt($curlobj, CURLOPT_INFILE, $fp);
curl_setopt($curlobj, CURLOPT_INFILESIZE, filesize($localfile));
$rtn = curl_exec($curlobj);  
fclose($fp); 
if(!curl_errno($curlobj)){
	echo "Uploaded successfully.";  
} else {
  echo 'Curl error: ' . curl_error($curlobj);
}
curl_close($curlobj);

*/

//案例六  访问https资源

$curlobj = curl_init();			// 初始化
curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.js");		// 设置访问网页的URL
curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);			// 执行之后不直接打印出来

// 设置HTTPS支持
date_default_timezone_set('PRC'); // 使用Cookie时，必须先设置时区
curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查从证书中检查SSL加密算法是否存在
curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, 2); // 

$output=curl_exec($curlobj);	// 执行
curl_close($curlobj);			// 关闭cURL
echo $output;









